package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("adminSetmealService")
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 启用或禁售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐业务
        //在起售套餐是判断是否有正在停售的菜品
        //判断是否处于起售状态
        if(status==StatusConstant.ENABLE) {
            //根据id得到菜品数据判断是否有菜品处于停售状态
            List<Dish> dishlist = dishMapper.getSetmealById(id);
            dishlist.forEach(dish->{
                if(dish.getStatus()==StatusConstant.DISABLE)
                    throw new DeletionNotAllowedException(MessageConstant.DISH_STOP_SALE);
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐业务
        //1.修改套餐表中的信息
        //2.修改套餐与菜品关系表中的信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除关系表中的信息
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //插入关系表中的信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(dish->
        {
            dish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }
    /**
     * 根据id得到套餐相关信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //根据id得到套餐信息业务
        //1.根据id查询得到套餐信息
        //2.根据套餐id查询得到套餐与菜品关系表中的信息
        Setmeal setmeal = setmealMapper.selectById(id);
        List<SetmealDish> list = setmealDishMapper.getSetmealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //删除套餐业务
        //起售中的套餐不能删除
        //1.删除套餐表中的信息
        //2.删除套餐与菜品关系表中的信息
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.selectById(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        ids.forEach(id->{
            setmealDishMapper.deleteBySetmealId(id);
            setmealMapper.deleteById(id);
        });
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //新增套餐业务
        //1.在套餐表插入套餐数据
        //2.在套餐与菜品关系表插入数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //通过主键返回得到要缓存的关系表中的Id
        Long setmealId = setmeal.getId();
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        //如果联系表中的菜品不为空
        if(dishes.isEmpty())
        {
            throw new IllegalArgumentException("套餐中的菜品不能为空");
        }
        if(!dishes.isEmpty()&&dishes.size()>0){
            dishes.forEach(dish->{
                dish.setSetmealId(setmealId);
            });
        }
        //批量插入
        setmealDishMapper.insertBatch(dishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //执行分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //查询得到数据
        Page<SetmealVO> setmeals = setmealMapper.page(setmealPageQueryDTO);
        //封装返回结果
        Long total = setmeals.getTotal();
        List<SetmealVO> records = setmeals.getResult();
        return new PageResult(total,records);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}

